#!/bin/sh

# $0 - shell file; $1 - dev/test/prod; $2 - hudson build

# --- setup ---

PROJECT_PATH=`cd \`dirname $0\`;cd ..;pwd`
TIMESTAMP=`date +%Y%m%d-%H%M`

JAVA_LIBS=.
for f in `ls ${PROJECT_PATH}/page/WEB-INF/lib/*.jar`;do
    JAVA_LIBS=${JAVA_LIBS}:${f}
done
for f in `ls ${PROJECT_PATH}/container-libs/*.jar`;do
    JAVA_LIBS=${JAVA_LIBS}:${f}
done

cd ${PROJECT_PATH}
rm -rf output
mkdir output
cd output

# --- copy and compile ---

cp -r ${PROJECT_PATH}/page .
rm -rf ./page/WEB-INF/classes
mkdir ./page/WEB-INF/classes
cp -r ${PROJECT_PATH}/java/config ./page/WEB-INF/classes/
#cp -r ${PROJECT_PATH}/petddframework/java/config ./WebRoot/WEB-INF/classes/
mv ./page/WEB-INF/classes/config/${1}/* ./page/WEB-INF/classes/config/
echo "version=${TIMESTAMP}.$2" >./page/WEB-INF/classes/config/version.properties

javac -encoding "utf-8" -cp ${JAVA_LIBS} -d ./page/WEB-INF/classes `find ${PROJECT_PATH}/java* -name *.java`
echo "api.${1}.${TIMESTAMP}.build.zip" >>./page/version.txt
rm -rf ./page/WEB-INF/classes/config/test
rm -rf ./page/WEB-INF/classes/config/prod

# --- pack ---

cd page
zip -r ${PROJECT_PATH}/output/api.${1}.${TIMESTAMP}.build.zip *
